<?php
class Slash_Debug 
{
	/*
   * Copyright © 2010 Jack Cleaver
   * 
   * This plugin provides functionality added by CalDAV (RFC 4791)
   * It implements new reports, and the MKCALENDAR method.
   *
   * This file is part of Slash.
   *
   * Slash is free software: you can redistribute it and/or modify
   * it under the terms of the GNU General Public License as published by
   * the Free Software Foundation, either version 3 of the License, or
   * (at your option) any later version.
   *
   * Slash is distributed in the hope that it will be useful,
   * but WITHOUT ANY WARRANTY; without even the implied warranty of
   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   * GNU General Public License for more details.
   *
   * You should have received a copy of the GNU General Public License
   * along with Slash.  If not, see <http://www.gnu.org/licenses/>.
	 */
	private static $DEBUG = true;
	
	public static function debug($msg)
	{
		if (self::$DEBUG)
		{
			ob_start();
			print "$msg\n";
			$str = ob_get_clean();
			$f = fopen("/home/jackc/sabredav/log.txt", "a");
			fwrite($f, $str);
			fclose($f);
		}
	}
	public static function stacktrace()
	{
		if (self::$DEBUG)
		{
			ob_start();
			debug_print_backtrace();
			$str = ob_get_clean();
			$f = fopen("/home/jackc/sabredav/log.txt", "a");
			fwrite($f, $str);
			fclose($f);
		}
	}
	public static function debugDump($msg, $var)
	{
		if (self::$DEBUG)
		{
			ob_start();
			print "$msg\n";
			var_dump($var);
			$str = ob_get_clean();
			$f = fopen("/home/jackc/sabredav/log.txt", "a");
			fwrite($f, $str);
			fclose($f);
		}
	}
	public static function debugBacktrace($msg)
	{
		if (self::$DEBUG)
		{
			ob_start();
			print "$msg\n";
			debug_print_backtrace();
			$str = ob_get_clean();
			$f = fopen("/home/jackc/sabredav/log.txt", "a");
			fwrite($f, $str);
			fclose($f);
		}
	}
	public static function debugDom($msg, $dom)
	{
		if (self::$DEBUG)
		{
			ob_start();
			print "$msg\n";
			if (get_class($dom) == "DOMDocument")
			{
				$doc = $dom;
			} else {
				$doc = $dom->ownerDocument;
			}
			$xml = $doc->saveXML($dom);
			print $xml;
			$str = ob_get_clean();
			$f = fopen("/home/jackc/sabredav/log.txt", "a");
			fwrite($f, $str);
			fclose($f);
		}
	}
	public static function dumpRequest(Sabre_HTTP_Request $req)
	{
		if (self::$DEBUG)
		{
			ob_start();
			print "\n" . $req->getMethod() . " " . $req->getUri() . "\n";
			foreach ($req->getHeaders() as $name=>$value)
			{
				print "$name: $value\n";
			}
			$fhBody = $req->getBody(false);
			$body = stream_get_contents($fhBody);
			$req->setBody($body);
			print $body . "\n";
			$str = ob_get_clean();
			$f = fopen("/home/jackc/sabredav/log.txt", "a");
			fwrite($f, $str);
			fclose($f);
		}
	}
}